
package com.hlkj.pay.controller.finance;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.finance.IFinanceRecordAppService;
import com.hlkj.pay.app.order.IPayOrderAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.finance.MerchantFinanceChargePageQueryDto;
import com.hlkj.pay.dto.finance.MerchantFinanceRecordPageQueryDto;
import com.hlkj.pay.infrastructure.model.finance.MerchantFinanceChargeDO;
import com.hlkj.pay.infrastructure.model.finance.MerchantFinanceRecordDO;
import com.hlkj.pay.infrastructure.model.pay.PayWayDO;
import com.hlkj.pay.service.finance.IMerchantFinanceChargeService;
import com.hlkj.pay.service.finance.IMerchantFinanceRecordService;
import com.hlkj.pay.service.pay.IPayWayService;
import com.hlkj.pay.vo.finance.req.MerchantFinanceChargePageReq;
import com.hlkj.pay.vo.finance.req.MerchantFinanceRecordCommonReq;
import com.hlkj.pay.vo.finance.req.MerchantFinanceRecordPageReq;
import com.hlkj.pay.vo.finance.resp.MerchantFinanceChargeResp;
import com.hlkj.pay.vo.finance.resp.MerchantFinanceRecordResp;
import com.hlkj.pay.vo.merchant.req.MerchantChargeReq;
import com.hlkj.pay.vo.openapi.order.req.PayOrderQueryReq;
import com.hlkj.pay.vo.openapi.order.resp.PayOrderSubmitApiResp;
import com.hlkj.pay.vo.order.resp.PayOrderResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author HlpayTeam
 * @date 2024/11/14 15:30
 */
@Tag(name = "商户财务账单")
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/v1/finance")
public class MerchantFinanceRecordController extends BaseController {

    private final IFinanceRecordAppService merchantFinanceRecordAppService;

    private final IMerchantFinanceRecordService merchantFinanceRecordService;

    private final IMerchantFinanceChargeService merchantFinanceChargeService;
    private final IPayOrderAppService payOrderAppService;

    private final IPayWayService payWayService;

    @Operation(summary = "账单流水列表")
    @GetMapping("/record")
    CommonResult<PageResult<MerchantFinanceRecordResp>> queryFinancePage(MerchantFinanceRecordPageReq merchantFinanceRecordPageReq) {
        MerchantFinanceRecordPageQueryDto financeRecordPageQueryDto = BeanUtil.copy(merchantFinanceRecordPageReq, MerchantFinanceRecordPageQueryDto.class);
        processQueryDate(merchantFinanceRecordPageReq, financeRecordPageQueryDto);
        PageResult<MerchantFinanceRecordDO> page = merchantFinanceRecordService.queryPage(financeRecordPageQueryDto);
        List<MerchantFinanceRecordDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<MerchantFinanceRecordResp> merchantFinanceRecordResps = cn.hutool.core.bean.BeanUtil.copyToList(list, MerchantFinanceRecordResp.class);
        processMerchantList(merchantFinanceRecordResps);
        return buildPageResult(page, merchantFinanceRecordResps);
    }

    @Operation(summary = "账单变动")
    @PostMapping("/record")
    CommonResult<Void> addFinanceRecord(@Validated @RequestBody MerchantFinanceRecordCommonReq merchantFinanceRecordCommonReq) {
        return merchantFinanceRecordAppService.addFinanceRecord(merchantFinanceRecordCommonReq);
    }

    @Operation(summary = "商户余额充值")
    @PostMapping("/charge")
    CommonResult<PayOrderSubmitApiResp> merchantChargePage(@RequestBody @Validated MerchantChargeReq merchantChargeReq) {
        return merchantFinanceRecordAppService.merchantCharge(merchantChargeReq);
    }
    @Operation(summary = "商户充值结果查询")
    @GetMapping("/charge/query")
    CommonResult<PayOrderResp> queryOrder(@RequestParam("payOrderNo") String payOrderNo) {
        PayOrderQueryReq payOrderQueryReq=new PayOrderQueryReq();
        payOrderQueryReq.setPayOrderNo(payOrderNo);
        return payOrderAppService.queryOrder(payOrderQueryReq);
    }

    @Operation(summary = "商户充值列表")
    @GetMapping("/charge")
    CommonResult<PageResult<MerchantFinanceChargeResp>> merchantChargePage(MerchantFinanceChargePageReq merchantFinanceChargePageReq) {
        MerchantFinanceChargePageQueryDto merchantFinanceChargePageQueryDto = BeanUtil.copy(merchantFinanceChargePageReq, MerchantFinanceChargePageQueryDto.class);
        processQueryDate(merchantFinanceChargePageReq, merchantFinanceChargePageQueryDto);
        PageResult<MerchantFinanceChargeDO> page = merchantFinanceChargeService.queryPage(merchantFinanceChargePageQueryDto);
        List<MerchantFinanceChargeDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }

        List<MerchantFinanceChargeResp> merchantFinanceRecordResps = cn.hutool.core.bean.BeanUtil.copyToList(list, MerchantFinanceChargeResp.class);
        processMerchantList(merchantFinanceRecordResps);
        List<String> codeList = list.stream().filter(baseResp -> StringUtils.hasText(baseResp.getPayWayCode())).map(roleResp -> roleResp.getPayWayCode()).collect(Collectors.toList());
        List<PayWayDO> payWayList = payWayService.queryByCodes(codeList);
        Map<String, PayWayDO> payWayMap = payWayList.stream().collect(Collectors.toMap(payWayDO -> payWayDO.getCode(), Function.identity(), (t1, t2) -> t1));
        //填充支付方式描述
        merchantFinanceRecordResps.stream().forEach(obj -> {
            if (StringUtils.hasText(obj.getPayWayCode())) {
                if (payWayMap.containsKey(obj.getPayWayCode())) {
                    obj.setPayWayCodeDesc(payWayMap.get(obj.getPayWayCode()).getName());
                }
            }
        });
        return buildPageResult(page, merchantFinanceRecordResps);
    }
}
